﻿#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using Pesta.DataAccess.Azure;
using Pesta.DataAccess.SqlServer;
using raya.Library;

namespace raya.Models.photos
{
    public class photos : Model
    {
        public int id { get; set; }
#if AZURE
        public string userid { get; set; }
#else
        public int userid { get; set; }
#endif
        public string url { get; set; }
        public imageType image_type { get; set; }

        public enum imageType
        {
            profile,
        }
        public photos()
        {
            image_type = imageType.profile;
        }
#if AZURE
        public static bool isImageBelongToUser(string userid, string imageid)
        {
            using (var db = RayaDataContext.DB())
            {
                var result = db.images.Where(x => x.id == imageid && x.userid == userid).SingleOrDefault();
                if (result == null)
                {
                    return false;
                }
                return true;
            }
        }

        public static void deleteImageFromDatabaseBasedOnImageID(string id)
        {
            using (var db = RayaDataContext.DB())
            {
                var data = db.images.Where(x => x.id == id).SingleOrDefault();
                if (data != null)
                {
                    db.DeleteOnSubmit(data);
                    db.SubmitChanges();
                }
            }
        }

        /// <summary>
        /// used in unit tests
        /// </summary>
        /// <param name="url"></param>
        public static void deleteImageFromDatabaseBasedOnUrl(string url)
        {
            using (var db = RayaDataContext.DB())
            {
                var data = db.images.Where(x => x.url == url).SingleOrDefault();
                if (data != null)
                {
                    db.DeleteOnSubmit(data);
                    db.SubmitChanges();
                }
            }
        }

        public string insertImageIntoDatabase()
        {
            using (var db = RayaDataContext.DB())
            {
                var img = new ImageRow(userid, DateTime.UtcNow.Ticks.ToString())
                              {
                                  image_type = (int)image_type, 
                                  url = url, 
                                  userid = userid
                              };
                db.InsertOnSubmit(AzureRayaDataContext.TableNames.images, img);
                db.SubmitChanges();
                return img.RowKey;
            }
        }

        public List<ImageRow> getImages(string uid)
        {
            using (var db = RayaDataContext.DB())
            {
                var result = db.images.Where(x => x.PartitionKey == uid);
                return result.ToList();
            }
        }
#else
        public static bool isImageBelongToUser(string userid, string imageid)
        {
            using (var db = RayaDataContext.DB())
            {
                var data = from i in db.images
                           where i.id.ToString() == imageid && i.userid.ToString() == userid
                           select i;
                if (data.Count() == 0)
                {
                    return false;
                }
                return true;
            }
        }
        public static void deleteImageFromDatabaseBasedOnImageID(string id)
        {
            using (var db = RayaDataContext.DB())
            {
                var data = (from i in db.images
                            where i.id.ToString() == id
                            select i).SingleOrDefault();
                if (data != null)
                {
                    db.images.DeleteOnSubmit(data);
                    db.SubmitChanges();
                }
            }
        }
        public string insertImageIntoDatabase()
        {
            using (var db = RayaDataContext.DB())
            {
                image data = new image {image_type = (int) image_type, url = url, userid = userid};
                db.images.InsertOnSubmit(data);
                db.SubmitChanges();
                return data.id.ToString();
            }
        }
        public List<image> getImages(int uid)
        {
            using (var db = RayaDataContext.DB())
            {
                var result = from i in db.images
                             where i.userid == uid
                             select i;
                //        new photos
                //          {id = i.id, userid = i.userid, image_type = (imageType) i.image_type, url = i.url};
                
                return result.ToList();
            }
            
        }
#endif

        
    }
}